package org.zcn.guomao_backend.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.zcn.guomao_backend.entity.Dept;
import org.zcn.guomao_backend.entity.Result;
import org.zcn.guomao_backend.service.IDeptService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author 张灿能
 * @since 2025-06-09
 */
@RestController
@RequestMapping("/depts")
@Tag(name = "部门管理")
public class DeptController {

    @Autowired
    private IDeptService deptService;


    // 请求部门数据
    @GetMapping()
    @Operation(summary = "获取部门列表", description = "获取所有部门的列表")
    public Result<List> list(){
        List<Dept> list = deptService.list();
        return Result.success(list);
    }

    // 新增部门
    @PostMapping
    @Operation(summary = "新增部门", description = "添加一个新的部门")
    public Result add(@RequestBody Dept dept){
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        boolean save = deptService.save(dept);
        if (save) {
            return Result.success();
        }
        return Result.fail("新增部门失败，请稍后再试！");
    }

    //修改部门
    @PutMapping
    @Operation(summary = "修改部门", description = "更新现有部门的信息")
    public Result update(@RequestBody Dept dept){
        dept.setUpdateTime(LocalDateTime.now());
        boolean update = deptService.updateById(dept);
        if (update) {
            return Result.success();
        }
        return Result.fail("修改部门失败，请稍后再试！");
    }

    //删除部门
    @DeleteMapping("/{id}")
    @Operation(summary = "删除部门", description = "根据部门ID删除部门")
    public Result delete(@PathVariable Long id){
        boolean remove = deptService.removeById(id);
        if (remove) {
            return Result.success();
        }
        return Result.fail("删除部门失败，请稍后再试！");
    }

}
